package com.mokairui.sort.advanced;

import java.util.Arrays;

/**
 * @Description
 * @Author Mokairui
 * @Since 2021/11/2
 */
public class Insertion {
    public static void main(String[] args) {
        int[] array = {8, 10, 11, 40, 22, 27, 2, 8, 18};
        sort(array);
        System.out.println(Arrays.toString(array));
    }

    private static void sort(int[] array) {
        // i 代表待插入元素的索引
        for (int i = 1; i < array.length; i++) {
            int t = array[i]; // 代表插入元素的值
            int j = i - 1; // 代表已经排序区域的元素索引
            while (j >= 0) {
                if (t < array[j]) {
                    array[j + 1] = array[j];
                } else {
                    break;
                }
                j--;
            }
            array[j + 1] = t;
        }
    }

    private static void swap(int[] array, int i, int j) {
        int temp = array[i];
        array[i] = array[j];
        array[j] = temp;
    }

}
